import os 
import shutil
subdirectories = ['ca' , 'certs', 'newcerts' ,'crl']
maindir ='X509CA'

def setup_directories():
    os.mkdir(maindir)
    for i in subdirectories:
        os.mkdir(os.path.join(maindir,i))

		
def create_files():
    mpath = os.getcwd()
    os.chdir(maindir)
    serial = open('serial', 'w')
    serial.write('01')
    serial.close()

    index = open('index.txt','w')
    index.close()
    os.chdir(mpath)

	
def compute_arg():
    return ''

	
def create_key_cert():
    os.system('openssl req -x509 -new -config openssl.cnf -days 365 -out X509CA/ca/ca_cert.pem\
	-keyout X509CA/ca/ca_pk.pem')

def create_pkcs12():
    os.system('openssl pkcs12 -export -in X509CA/ca/ca_cert.pem -inkey X509CA/ca/ca_pk.pem -out mycakeystore.p12 -name cacert')
	
def create_jks():
    os.system('keytool -importkeystore -srckeystore mycakeystore.p12 -srcstoretype PKCS12\
	-deststoretype JKS -destKeystore mycakeystore.jks')
def export_cert():
    os.system('keytool -export -file pkw.cer -keystore mycakeystore.jks -file pkw.cer -alias cacert')

def copy_cert():
    shutil.copy(os.path.join(maindir,'ca','ca_cert.pem'),os.getcwd())
def main():
    #print os.name
    #setup_directories()
	#print 'Created directoiries'
    #create_files()
	#print 'Created files'
    #create_key_cert()
	#print 'Created private key and certificate'
    #copy_cert()
	#print 'Copyed cacert to base catalog'
    #create_pkcs12()
	#print 'Created PKCS12 keystore'
    #create_jks()
	#print 'Created mycakeystore.jks certificate of PKW has alias cacert'
	export_cert()
	

if  __name__ == "__main__":
    main()